import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:industry_net/base/common/util.dart';
import 'package:industry_net/base/common/route/routes.dart';
import 'package:industry_net/base/component/list/section.dart';
import 'package:industry_net/pages/home/app_more/models/app_model.dart';
import 'package:industry_net/pages/home/app_more/state.dart';
import 'package:industry_net/pages/home/app_more/views/cell.dart';
import 'package:industry_net/pages/home/main/models/home_menu.dart';

class AppMoreController extends GetxController {
  final state = AppMoreState();
  @override
  void onInit() {
    super.onInit();
    observer();
    getData();
    getCollectData();
    Get.log('onInit');
  }

  @override
  void onReady() {
    super.onReady();
    Get.log('onready');
    observer();
  }

  void observer() {}

  void getCollectData() {
    state.collectList.value = [
      HomeMenuModel()
        ..icon = 'menu_icon_Mall.png'.imgPathHome
        ..name = '优选商城',
      HomeMenuModel()
        ..icon = 'menu_icon_financial.png'.imgPathHome
        ..name = '财政补贴',
      HomeMenuModel()
        ..icon = 'menu_icon_pear.png'.imgPathHome
        ..name = '现代梨园',
    ];
  }

  void getData() {
    var items1 = [
      AppModel.fromJson(
        {
          'title': '优选商城',
          'items': [
            {'icon': 'menu_icon_Mall.png'.imgPathHome, 'name': '优选商城'},
            {'icon': 'menu_icon_financial.png'.imgPathHome, 'name': '发布商品'},
            {'icon': 'menu_icon_pear.png'.imgPathHome, 'name': '店铺管理'},
          ]
        },
      )
    ];
    var sec1 = SectionModel(
        index: 0,
        itemBuilder: (context, index) {
          return AppMoreCell(mod: items1[index]);
        },
        items: items1);

    var items2 = [
      AppModel.fromJson(
        {
          'title': '财政补贴',
          'items': [
            {'icon': 'menu_icon_financial.png'.imgPathHome, 'name': '财政补贴'},
            {'icon': 'menu_icon_financial.png'.imgPathHome, 'name': '财政管理'},
          ]
        },
      )
    ];
    var sec2 = SectionModel(
        index: 0,
        itemBuilder: (context, index) {
          return AppMoreCell(mod: items2[index]);
        },
        items: items2);

    var items3 = [
      AppModel.fromJson(
        {
          'title': '现代梨园',
          'items': [
            {'icon': 'menu_icon_pear.png'.imgPathHome, 'name': '现代梨园'},
            {'icon': 'menu_icon_financial.png'.imgPathHome, 'name': '实施灌溉'},
            {'icon': 'menu_icon_pear.png'.imgPathHome, 'name': '自动施肥'},
          ]
        },
      )
    ];
    var sec3 = SectionModel(
        index: 0,
        itemBuilder: (context, index) {
          return AppMoreCell(mod: items3[index]);
        },
        items: items3);

    var items4 = [
      AppModel.fromJson(
        {
          'title': '田头市场',
          'items': [
            {'icon': 'menu_icon_field.png'.imgPathHome, 'name': '田头市场'},
            {'icon': 'menu_icon_financial.png'.imgPathHome, 'name': '市场管理'},
          ]
        },
      )
    ];
    var sec4 = SectionModel(
        index: 0,
        itemBuilder: (context, index) {
          return AppMoreCell(mod: items4[index]);
        },
        items: items4);

    var items5 = [
      AppModel.fromJson(
        {
          'title': '供需大厅',
          'items': [
            {'icon': 'menu_icon_supply.png'.imgPathHome, 'name': '供需大厅'},
            {'icon': 'menu_icon_financial.png'.imgPathHome, 'name': '供应大厅'},
            {'icon': 'menu_icon_pear.png'.imgPathHome, 'name': '求购大厅'},
            {'icon': 'menu_icon_pear.png'.imgPathHome, 'name': '供需管理'},
          ]
        },
      )
    ];
    var sec5 = SectionModel(
        index: 0,
        itemBuilder: (context, index) {
          return AppMoreCell(mod: items5[index]);
        },
        items: items5);

    var items6 = [
      AppModel.fromJson(
        {
          'title': '金融服务',
          'items': [
            {'icon': 'menu_icon_Mall.png'.imgPathHome, 'name': '金融服务'},
            {'icon': 'menu_icon_financial.png'.imgPathHome, 'name': '贷款'},
          ]
        },
      )
    ];
    var sec6 = SectionModel(
        index: 0,
        itemBuilder: (context, index) {
          return AppMoreCell(mod: items6[index]);
        },
        items: items6);
    var items7 = [
      AppModel.fromJson(
        {
          'title': '金融服务',
          'items': [
            {'icon': 'menu_icon_Mall.png'.imgPathHome, 'name': '金融服务'},
            {'icon': 'menu_icon_financial.png'.imgPathHome, 'name': '贷款'},
          ]
        },
      )
    ];
    var sec7 = SectionModel(
        index: 0,
        itemBuilder: (context, index) {
          return AppMoreCell(mod: items7[index]);
        },
        items: items7);
    state.dataSource = [sec1, sec2, sec3, sec4, sec5, sec6, sec7];
  }

  void actionManage() async {
    var result = await Get.toNamed(RouterPath.app_man,
        arguments: {'collect': state.collectList.value});
    if (result != null) {
      state.collectList.value = result;
      update();
    }
  }

  void actionSearch() {
    Get.toNamed(RouterPath.search);
  }

  void actionTab(int index) {
    var index = state.currentIndex.value;
    var mod = state.dataSource[index].items.first as AppModel;
    Scrollable.ensureVisible(mod.key.currentContext!);
  }

  @override
  void onClose() {
    state.tabCtrl.dispose();
    super.onClose();
  }
}
